package com.uni.crawl.service;

import cn.hutool.core.bean.BeanUtil;
import com.uni.crawl.common.enums.crawl.CrawlInstanceStatusEnum;
import com.uni.crawl.model.entity.crawl.CrawlInstance;
import com.uni.crawl.model.entity.crawl.CrawlTemplate;
import com.uni.crawl.service.crawl.CrawlInstanceService;
import com.uni.crawl.service.crawl.CrawlTemplateService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class CrawlTemplateCmd {

    private final CrawlTemplateService crawlTemplateService;
    private final CrawlInstanceService crawlInstanceService;

    public Boolean run(Long templateId) {
        CrawlTemplate tmpl = crawlTemplateService.getById(templateId);
        if (tmpl == null) return false;

        CrawlInstance ins = BeanUtil.toBean(tmpl, CrawlInstance.class);
        ins.setId(null);
        ins.setInstanceStatus(CrawlInstanceStatusEnum.INIT.getStatus());
        ins.setQueueTotal(1);

        crawlInstanceService.save(ins);
        return true;
    }


}
